package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Group_member_Dao {
	
	private Chat_Group_member group_member;
	final static String TABLE_NAME = "group_member";
	static PreparedStatement ps = null;
	static ResultSet rs = null;
	static Connection con = BaseConnection.getConnection();

	
	public static void modify_Member_name(String member_name,int group_id,int user_id) {// 修改用户群内名称
		String sql = "updata "+TABLE_NAME+" set  member_name= '"+member_name+"' where group_id = '"+group_id+"' and user_id='"+user_id+"'";
		try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}	 	
	
	public static void modify_Last_read_message_id(long last_read_message_id,int group_id,int user_id) {//修改最后已读群聊信息
		String sql = "updata "+TABLE_NAME+" set  last_read_message_id= '"+last_read_message_id+"' where group_id = '"+group_id+"' and user_id='"+user_id+"'";
		try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}	 
	
	public static void modify_Identity(String identity,int group_id,int user_id) {//修改用户群内身份
		String sql = "updata "+TABLE_NAME+" set  identity= '"+identity+"' where group_id = '"+group_id+"' and user_id='"+user_id+"'";
		try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}	 
	public static void modify_Group_alias(String group_alias,int group_id,int user_id) {//修改群的别名
		String sql = "updata "+TABLE_NAME+" set  group_alias= '"+group_alias+"' where group_id = '"+group_id+"' and user_id='"+user_id+"'";
		try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}	 
	
	public static String search_Member_name(int group_id,int user_id) {//查找用户群内名称
		String temp = null;
		ResultSet r = searchOne(group_id, user_id);
		try {
			r.next();
			temp = r.getString(4);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return temp;
	}
	
	public static String search_Identity (int group_id,int user_id) {// 查找用户群内身份
		String temp = null;
		ResultSet r = searchOne(group_id, user_id);
		try {
			r.next();
			temp = r.getString(3);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return temp;
	}
	
	public static String search_Group_alias(int group_id,int user_id) {// 查找群的别名
		String temp = null;
		ResultSet r = searchOne(group_id, user_id);
		try {
			r.next();
			temp = r.getString(5);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return temp;
	}
	
	public static long search_Last_read_message_id (int group_id,int user_id) {// 查找最后已读群聊信息
		long temp =  -1;
		ResultSet r = searchOne(group_id, user_id);
		try {
			r.next();
			temp = r.getLong(6);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return temp;
	}
	
	public static ResultSet searchOne(int group_id , int user_id) {		//用做调用
		String sql = "select * from "+TABLE_NAME+" where group_id = '"+group_id+"' and user_id = '"+user_id+"'";
		try {
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
		} catch (SQLException e) {
 			e.printStackTrace();
		}
		 return rs;
	}	
	
	public ResultSet searchAll(int user_id) {		//查找某个用户加入的所有群
		String sql = "select * from "+TABLE_NAME+" where user_id = '"+user_id+"'";
		try {
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
		} catch (SQLException e) {
 			e.printStackTrace();
		}
		 return rs;
	}	
	public void addOne(int group_id,int user_id,String identity,String member_name,String group_alias,long read) {
		group_member = new Chat_Group_member();
		group_member.setGroup_id(group_id);
		group_member.setUser_id(user_id);
		group_member.setIdentity(identity);
		group_member.setMember_namne(member_name);
		group_member.setGroup_alias(group_alias);
		group_member.setLast_read_message_id(read);
		addOne();
	}
	
	public void addOne() {					//添加某个群成员				 
		String sql = "insert into "+TABLE_NAME+"(group_id,user_id,identity,member_name,group_alias,last_read_message_creation_time) values( '"+group_member.getGroup_id()+"','"+group_member.getUser_id()+"','"+group_member.getIdentity()+"','"+group_member.getMember_name()+"','"+group_member.getGroup_alias()+"','"+group_member.getLast_read_message_id()+"')";
		try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void update() {									//更新数据
		String sql = "update "+TABLE_NAME+" set identity = '"+group_member.getIdentity()+"', member_name = '"+group_member.getMember_name()+"',group_alias='"+group_member.getGroup_alias()+"', last_read_message_id = '"+group_member.getLast_read_message_id()+"' where group_id = '"+group_member.getGroup_id()+"' and user_id = '"+group_member.getGroup_id()+"'";;
		try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void deleteOne(int group_id,int user_id) {                                //删除某个群成员
		String sql = "delete from "+TABLE_NAME+" where group_id = '"+group_id+"' and user_id = '"+user_id+"'";
		try {
			ps = con.prepareStatement(sql);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
	
	public void deleteGroup(int group_id) {								//删除某个群所有成员
		String sql = "delete from "+TABLE_NAME+" where goup_id = '"+group_id+"'";
		try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}

	public Chat_Group_member getGroup_member() {
		return group_member;
	}

	public void setGroup_member(Chat_Group_member group_member) {
		this.group_member = group_member;
	}
}
